//
//  ZJShouCangFangYuanVC.m
//  英伦府邸
//
//  Created by IOS_DEVELOPER on 2017/8/11.
//  Copyright © 2017年 英伦府邸. All rights reserved.
//

#import "ZJShouCangFangYuanVC.h"
#import "ZJRoomModel.h"
#import "ZJShouCangCell.h"
#import "ZJlockRoomVC.h"
@interface ZJShouCangFangYuanVC (){
    NSInteger dateResult;
}
@property (strong, nonatomic)NSMutableArray * modelArr;
@property (strong, nonatomic)UIAlertController *alertController;
@end

@implementation ZJShouCangFangYuanVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self sendRequest];
    [self.mm_drawerController closeDrawerAnimated:YES completion:^(BOOL finished) {
        //设置打开抽屉模式为MMOpenDrawerGestureModeNone，也就是没有任何效果。
        [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
    }];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ZJShouCangCell class]) bundle:nil] forCellReuseIdentifier:@"ZJShouCangCell"];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
[tableView tableViewDisplayWitMsg:kNoDataImgName ifNecessaryForRowCount:self.modelArr.count];
    return self.modelArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZJShouCangCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZJShouCangCell"];
    ZJRoomModel * model = self.modelArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = model;
    cell.backgroundColor = [UIColor clearColor];

    [cell useCellFrameCacheWithIndexPath:indexPath tableView:tableView];
    
    return cell;
}
-  (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
-(NSString*)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexpath {
    
    return @"            ";
    
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView :(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    /**   点击 删除 按钮的操作 */
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        ZJRoomModel * model = self.modelArr[indexPath.row];
        [HUD showLoading:@"删除中..."];
        NSDictionary *parameter = [LRNetwork requestParameter:@{@"house_id":model.house_id}];
        NSString *url = [LRNetwork requestURL:@"cgi/house_select_del.php"];
        
        [LRNetwork netRequestPOSTWithRequestURL:url withParameter:parameter withReturnValeuBlock:^(id returnValue) {
            [self.modelArr removeObject:model];
            [HUD showSucceedMsg:@"删除成功"];
            [self.tableView reloadData];
            [HUD hide];
        } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
            [HUD showErrorMsg:errorMessage];
        } withFailureBlock:^{
            
        }];
    }

}
// iOS11方法
- ( UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
    //删除
    UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        ZJRoomModel * model = self.modelArr[indexPath.row];
        [HUD showLoading:@"删除中..."];
        NSDictionary *parameter = [LRNetwork requestParameter:@{@"house_id":model.house_id}];
        NSString *url = [LRNetwork requestURL:@"cgi/house_select_del.php"];
        
        [LRNetwork netRequestPOSTWithRequestURL:url withParameter:parameter withReturnValeuBlock:^(id returnValue) {
            [self.modelArr removeObject:model];
            [HUD showSucceedMsg:@"删除成功"];
            [self.tableView reloadData];
            [HUD hide];
        } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
            [HUD showErrorMsg:errorMessage];
        } withFailureBlock:^{
            
        }];
        completionHandler (YES);
    }];
    deleteRowAction.image = [UIImage imageNamed:@"hs_gb"];
    deleteRowAction.backgroundColor = [UIColor lightGrayColor];
    
    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
    return config;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //时间计算
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
    
    [dateFormat setDateFormat:@"yyyy-MM-dd"];
    
    
    dateFormat.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    [dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    NSDate *localeDate =[dateFormat dateFromString:self.proModel.net_sale_start];
    
    //系统当前时间
    NSDate *nowDate = [Utils getInternetDate];
    
    dateResult = [Utils compareOneDay:nowDate withAnotherDay:localeDate];
    
    if (dateResult == -1) {
        [HUD showMsg:@"未到购房时间"];
        return;
    }
    
    self.alertController = [UIAlertController alertControllerWithTitle:@"注意" message:@"您只能抢购一套房子，是否确认抢购当前户型？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *resetAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [self.alertController dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [self.alertController addAction:resetAction];
    UIAlertAction *resetAction1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        ZJRoomModel * model = self.modelArr[indexPath.row];
        [self sureBuyHouse:model];
    }];
    
    [self.alertController addAction:resetAction1];
    [self presentViewController:_alertController animated:YES completion:nil];
}
-(void)sendRequest{
    [HUD showLoading:@"加载数据中..."];
    NSDictionary *parameter = [LRNetwork requestParameter:@{}];
    NSString *url = [LRNetwork requestURL:@"cgi/house_select_list.php"];
    
    [LRNetwork netRequestPOSTWithRequestURL:url withParameter:parameter withReturnValeuBlock:^(id returnValue) {
        self.modelArr = [ZJRoomModel mj_objectArrayWithKeyValuesArray:returnValue[@"house_selected"]];

        [self.tableView reloadData];
        [HUD hide];
    } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
        [HUD showErrorMsg:errorMessage];
    } withFailureBlock:^{
        
    }];
}
-(void)sureBuyHouse:(ZJRoomModel *)model{
    [HUD showLoading:@"抢购中..."];
    ZJlockRoomVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJlockRoomVC"];
    
    NSDictionary *parameter = [LRNetwork requestParameter:@{@"house_id":model.house_id}];
    NSString *url = [LRNetwork requestURL:@"cgi/house_buy.php"];
    
    [LRNetwork netRequestPOSTWithRequestURL:url withParameter:parameter withReturnValeuBlock:^(id returnValue) {
        
        [HUD showSucceedMsg:@"购房成功!"];
        vc.model = model;
        ZJUserModel * userModel = GET_USERINFO;
        vc.userModel = userModel;
        [self.navigationController pushViewController:vc animated:YES];
        
    } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
        [HUD showErrorMsg:errorMessage];
    } withFailureBlock:^{
        
    }];
}
@end
